<template>
  <div class="login-bg" :style="'background-image:url(' + bgimg_url + ');'">
    <el-form
      :model="ruleForm2"
      :rules="rules2"
      ref="ruleForm2"
      label-position="left"
      label-width="0px"
      class="demo-ruleForm login-container"
    >
      <h3 class="title">{{adminName || "Saas運營管理系統"}}</h3>
      <el-form-item prop="account">
        <el-input
          type="text"
          v-model="ruleForm2.account"
          auto-complete="off"
          placeholder="賬號"
        ></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm2.checkPass"
          auto-complete="off"
          placeholder="密碼"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code" style="line-height: 0px" v-if="codeImage">
        <div class="d-b-c ww100">
          <div class="left-img-input" style="width: auto; margin-bottom: 0">
            <el-input
              v-model="ruleForm2.code"
              ref="code"
              placeholder="驗證碼"
              class="l-input"
              style="width: 170px"
            ></el-input>
          </div>
          <div
            class="identifybox"
            style="height: 30px; flex-shrink: 0"
            @click="getData"
          >
            <el-image
              class="codeImage"
              style="width: 93px; height: 30px"
              :src="codeImage"
            />
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="width: 100%"
          @click="handleSubmit2"
          :loading="logining"
          >登錄</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { useUserStore } from "@/store";
import UserApi from "@/api/user.js";
const {
  afterLogin,
  token
} = useUserStore();
export default {
  data() {
    return {
      token,
      logining: false,
      ruleForm2: {
        account: "",
        checkPass: "",
        code: ""
      },
      rules2: {
        account: [{
          required: true,
          message: "請輸入賬號",
          trigger: "blur"
        }],
        checkPass: [{
          required: true,
          message: "請輸入密碼",
          trigger: "blur"
        }],
        code: [{
          required: true,
          message: "請輸入驗證碼",
          trigger: "blur"
        }]
      },
      checked: true,
      codeImage: "",
      codeKey: "",
      adminName: "",
      bgimg_url: ""
    };
  },
  created() {
    this.getData();
  },
  methods: {
    /*获取基础配置*/
    getData() {
      let self = this;
      UserApi.base(true).then(res => {
        self.loading = false;
        const {
          data
        } = res;
        self.codeImage = data.codeImage;
        self.codeKey = data.codeKey;
        self.adminName = data.adminName;
        if (data.adminBgImg) {
          self.bgimg_url = data.adminBgImg;
        } else {
          self.bgimg_url = "";
        }
      }).catch(error => {
        self.loading = false;
      });
    },
    handleReset2() {
      this.$refs.ruleForm2.resetFields();
    },
    handleSubmit2(ev) {
      var _this = this;
      this.$refs.ruleForm2.validate(valid => {
        if (valid) {
          this.logining = true;
          var loginParams = {
            username: this.ruleForm2.account,
            password: this.ruleForm2.checkPass,
            code: this.ruleForm2.code,
            codeKey: this.codeKey
          };
          UserApi.login(loginParams, true).then(async res => {
            this.logining = false;
            if (res.code == 1) {
              await afterLogin(res);
              this.logining = false;
              this.$router.push({
                path: "/home"
              });
            } else {
              ElMessage({
                message: "登錄失敗",
                type: "error"
              });
            }
          }).catch(error => {
            this.getData();
            this.logining = false;
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login-bg {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100%;
}

.login-container {
  box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #323a4a;
  border: 1px solid #384050;
  box-shadow: 0 0 12px 0 rgba(0, 0, 0, 0.4);

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #ffffff;
    font-size: 16px;
  }

  .remember {
    margin: 0px 0px 35px 0px;
  }
}
</style>

